<template>
  <el-dialog
    title="服务事项"
    :visible.sync="dialogValue"
    width="900px"
    append-to-body
  >
    <div class="search-params f-r a-c j-b">
      <el-form inline>
        <el-form-item label="中介名称：">
          <el-select v-model="searchParams.queryName" style="width: 400px" filterable clearable>
            <el-option v-for="item in options" :key="item.id" :value="item.id" :label="item.serviceMatters" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" round @click="onAdd">添加</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="table-box f-g">
      <el-table
        v-loading="loading"
        height="350px"
        stripe
        :data="tableData"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        @selection-change="onSelect"
      >
        <el-table-column type="index" width="80px" label="序号" align="center" />
        <el-table-column v-for="item in columns" :key="item.prop" :prop="item.prop" :label="item.label" align="center" />
        <el-table-column label="操作" fixed="right" width="60">
          <template slot-scope="scope">
            <el-button type="text" @click="onDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-box">
      <el-pagination
        background
        :current-page="pageNo"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalSize"
        @size-change="changePageSize"
        @current-change="changePageNo"
      />
    </div>
  </el-dialog>
</template>

<script>

import crud from '@/mixins/crud'
import { http_intermediary_serveevent } from '@/api'

export default {
  name: 'ServeEvent',
  mixins: [crud],
  data() {
    return {
      request: http_intermediary_serveevent,
      searchParams: {
        queryName: ''
      },
      columns: [
        { prop: 'serviceMatters', label: '事项名称' },
        { prop: 'serviceCode', label: '事项编码' },
        { prop: 'approvalDepartment', label: '审批部门' },
        { prop: 'createTime', label: '创建时间' }
      ],
      options: [],
      dialogValue: false,
      intermediaryId: ''
    }
  },
  created() {
    this.request.allList().then(res => {
      this.options = res.data
    })
  },
  methods: {
    open(item) {
      this.dialogValue = true
      this.intermediaryId = item.id
      this.onSearch()
    },
    getQueryParams() {
      const params = {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        intermediaryId: this.intermediaryId
      }
      return params
    },
    initData() {
      if (!this.intermediaryId) return
      const params = this.getQueryParams()
      this.request.page(params).then(res => {
        console.log(res)
        this.tableData = res.data.records
        this.totalSize = res.data.total || 0
      })
    },
    onAdd() {
      this.request.create({
        intermediaryId: this.intermediaryId,
        serviceMattersId: this.searchParams.queryName
      }).then(res => {
        this.initData()
        this.$message.success(res.msg)
        this.searchParams.queryName = ''
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
